package toy.keli.picmap.model;

import android.os.CountDownTimer;
import android.os.Handler;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

//Java 日期时间  https://www.runoob.com/java/java-date-time.html
public class time extends Date{
    Date data;
    public String formatString="yyyy-MM-dd HH:mm:ss";

    public time(String input){
        super();
        init(input,formatString);
    }
    public time(String input, String format){
        super();
        init(input,format);
    }

    public time(long t) {
        super();
        setTime(t);
    }
    public time() {
        super();
    }

    public time set(long t) {
        this.setTime(t);
        return this;
    }

    Calendar getCalendar(){
        Calendar c= Calendar.getInstance();
        c.setTime(this);

        return c;
    }
    void init(String input,String format){
        try {
            data = getFormat(format).parse(input);
        } catch (ParseException e) {
            e.printStackTrace();
            data = new Date();
            data.setTime(0);
        }
        this.setTime(data.getTime());
    }
    SimpleDateFormat getFormat(String format){
        return new SimpleDateFormat(format);
    }
    public String format(){
        return format(formatString);
    }
    public String format(String demo){
        return getFormat(demo).format(this);
    }


    public int getWeek() {
        int day=getCalendar().get(Calendar.DAY_OF_WEEK);
        return day==1? 7:day-1;
    }

    // 一年中第几周的开始时间
    def mwMap=new def.dict("周一",0,"周二",1,"周三",2,"周四",3,"周五",4,"周六",5,"周日",6);
    public time setWeek(int i) {
        long start = this.getTime("yyyy-MM-dd") - (int)mwMap.get(this.format("E")).source() * 86400000;
        long wn = i - this.formatInt("w");
        this.setTime(start+wn*86400*1000*7);
        return this;
    }
    //获取本周时间开始时间
    public long getwTime() {
        int d2w=getWeek()-1;
        return this.getTime("yyyy-MM-dd")-86400000*d2w;
    }
    //获取开始时间  比如一天
    public long getTime(String s) {
        return new time(format(s),s).getTime();
    }
    public long getTime(String s,String f) {
        return new time(format(s),f).getTime();
    }
    public time next(String type) {
        return next(type,1);
    }
    public int[] getSeason(){
        int t0=0,t1 = 0,n=0;
        switch (this.formatInt("MM")){
            case 12:
            case 1:
            case 2:
                t0=12; t1=3; n=4;
                break;
            case 3:
            case 4:
            case 5:
                t0=3; t1=6; n=1;
                break;
            case 6:
            case 7:
            case 8:
                t0=6; t1=9; n=2;
                break;
            case 9:
            case 10:
            case 11:
                t0=9; t1=12;  n=3;
                break;
        }

        return new int[]{n,t0,t1};
    }
    public time next(String type,int n) {
        long t=this.getTime(),day=86400*1000;

        switch (type){
            case "year":
                t=new time(String.valueOf(Integer.valueOf(this.format("yyyy"))+n),"yyyy").getTime();
                break;
            case "season":
                int[] a = getSeason();
                int t0=a[1],t1 = a[2];
                this.setTime(this.getTime("yyyy-"+t0,"yyyy-MM"));
                n*=3;
            case "mouth":
                int now=(this.formatInt("MM")+n)+this.formatInt("yyyy")*12;
                t=new time(""+(now/12)+now%12,"yyyyMM").getTime();
               break;
            case "week":
                t+=n*day*7;
                break;
            case  "day":
                t+=n*day;
                break;
        }
        this.setTime(t);
        return this;
    }

    public int formatInt(String type) {
        return Integer.valueOf(format(type));
    }

    public void reset() {
        this.setTime(data==null? new Date().getTime() : data.getTime());
    }

    public void setSeason(int i) {
        int[] nowS = getSeason();
        long start = getTime("yyyy-MM");
        int n = i - nowS[0];
        start =new time(start+n*86400*1000*30*n).getTime("yyyy-MM");
        this.setTime(start);
    }

    public String getSeasonName() {
        String[] map=new String[]{"","春季","夏季","秋季","冬季"};
        return map[getSeason()[0]];
    }


    public interface lambdas{
        void call();
    }
    public interface lambdasIndex{
        boolean call(int index);
    }
    public interface lambdasObj{
        void call(Object o);
    }
    //timeout有点像是 timeInterval
    public static void Out(int time,lambdas f){
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                f.call();
            }
        }, time);    //延时1s执行
    }

    public static Timer loop(int time,lambdasIndex f){
        Timer timer = new Timer();
        final int[] index = {0};
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                boolean rt = f.call(index[0]++);
                if(rt==false) timer.cancel();
            }
        },time);//延时1s执行
        return timer;
    }


    public static CountDownTimer forEach(int time, def a,lambdasObj f){
        CountDownTimer cdt = new CountDownTimer(a.size()*time, time) {
            @Override
            public void onTick(long millisUntilFinished) {
                f.call(a.next());
            }
            @Override
            public void onFinish() {
                f.call(null);
            }
        };

        cdt.start();
        return cdt;
    }

    /**
     * 时间执行
     * @param maxT  最大时间
     * @param t     每次调用间隔的执行之间
     * @param o     每次间隔的执行函数
     * @param o1    最大时间执行函数
     */
    public static void run(int maxT, int t, lambdasIndex o, lambdas o1) {
        final int[] index = {0};
        CountDownTimer cdt = new CountDownTimer(maxT, t) {
            @Override
            public void onTick(long millisUntilFinished) {
                boolean rt = o.call(index[0]++);
                if(!rt) {
                    this.onFinish();
                    this.cancel();
                }
            }
            @Override
            public void onFinish() {
                o1.call();
            }
        };

        cdt.start();
    }
    public static void sleep(int t){
        usleep(t*1000);//延时1s
    }

    public static void usleep(int t){
        try {
            Thread.sleep(t);//延时1s
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    //同类限制
    public long[] sameLimit(String name){
        time mTime = this;
        long timeStamp = mTime.getTime("yyyy-MM-dd");
        long day=86400*1000;
        long[] cDate = new long[3];
        switch (name){
            case "同周":
                cDate=new long[]{mTime.getwTime(),mTime.getwTime()+7*day,2};
                break;
            case "同月":
                int m = mTime.formatInt("MM") + 1;
                String format = (m < 13) ? "yyyy-" + m + "-01" : "yyyy-12-31 23:59:59";
                cDate=new long[]{mTime.getTime("yyyy-MM-01","yyyy-MM-dd"),mTime.getTime(format,"yyyy-MM-dd"),1};
                break;
            case "同季":
                String m1 = mTime.format("MM");
                def map=new def.dict(
                        "03,04,05",new long[]{mTime.getTime("yyyy-03-01","yyyy-MM-dd"),mTime.getTime("yyyy-06-01","yyyy-MM-dd"),1},
                        "06,07,08",new long[]{mTime.getTime("yyyy-06-01","yyyy-MM-dd"),mTime.getTime("yyyy-09-01","yyyy-MM-dd"),1},
                        "09,10,11",new long[]{mTime.getTime("yyyy-09-01","yyyy-MM-dd"),mTime.getTime("yyyy-12-01","yyyy-MM-dd"),1},
                        "12,01,02",new long[]{mTime.getTime("yyyy-12-01","yyyy-MM-dd"),mTime.getTime((mTime.formatInt("yyyy")+1)+"-03-01","yyyy-MM-dd"),1}
                );
                for(def k:map) {
                    if(k.toString().indexOf(m1)>-1) {
                        cDate=(long[])map.get(k).source();
                        break;
                    }
                }
                break;
            case "同年":
                cDate=new long[]{mTime.getTime("yyyy-01-01","yyyy-MM-dd"),mTime.getTime("yyyy-12-31 23:59:59","yyyy-MM-dd hh:mm:ss"),1};
                break;
            case "同天":
            default:
                cDate=new long[]{timeStamp,timeStamp+day,3};
        }

        return cDate;
    }
}
